package com.pdg.mcplugin.payd2mine;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;

import com.pdg.mcplugin.common.baseclasses.PluginClientBase;

public class ConfigurationManager extends PluginClientBase<Payd2Mine> {

	private static final String PATH_UPDATER_DELAY = "updater.delay";
	private static final long UPDATER_DELAY_DEFAULT = 0;
	private static final String PATH_UPDATER_PERIOD = "updater.period";
	private static final long UPDATER_PERIOD_DEFAULT = 2500;
	private static final String PATH_COOLDOWN_PERIOD = "cooldown.period";
	private static final long COOLDOWN_PERIOD_DEFAULT = 150000;
	private static final String SECTION_MATERIALS = "materials";
	private static final String PATH_MATERIAL = "material";
	private static final String PATH_AMOUNT = "amount";
	private static final String PATH_DEFAULT_AMOUNT = "defaultAmount";
	private static final double DEFAULT_AMOUNT_DEFAULT = 0;
	private Long updaterDelay=null;
	private Long updaterPeriod=null;
	private Long cooldownPeriod;
	private Double defaultAmount;

	public ConfigurationManager(Payd2Mine thePlugin) {
		super(thePlugin);
	}

	public long getUpdaterDelay() {
		if(updaterDelay==null){
			updaterDelay = getPlugin().getConfig().getLong(PATH_UPDATER_DELAY, UPDATER_DELAY_DEFAULT);
		}
		return updaterDelay;
	}

	public long getUpdaterPeriod() {
		if(updaterPeriod==null){
			updaterPeriod = getPlugin().getConfig().getLong(PATH_UPDATER_PERIOD, UPDATER_PERIOD_DEFAULT);
		}
		return updaterPeriod;
	}

	public long getCooldownPeriod() {
		if(cooldownPeriod==null){
			cooldownPeriod = getPlugin().getConfig().getLong(PATH_COOLDOWN_PERIOD, COOLDOWN_PERIOD_DEFAULT);
		}
		return cooldownPeriod;
	}

	public Map<Material, Double> getMaterialTable() {
		Map<Material,Double> materialTable = new HashMap<Material,Double>();
		ConfigurationSection theSection = getPlugin().getConfig().getConfigurationSection(SECTION_MATERIALS);
		if(theSection!=null){
			Set<String> theKeys = theSection.getKeys(false);
			if(theKeys!=null){
				Iterator<String> iterator = theKeys.iterator();
				while(iterator.hasNext()){
					String theKey = iterator.next();
					Material theMaterial = Material.valueOf(theSection.getConfigurationSection(theKey).getString(PATH_MATERIAL));
					double theAmount = theSection.getConfigurationSection(theKey).getDouble(PATH_AMOUNT);
					materialTable.put(theMaterial, theAmount);
				}
			}
		}
		return materialTable;
	}

	public double getDefaultAmount() {
		if(defaultAmount==null){
			defaultAmount = getPlugin().getConfig().getDouble(PATH_DEFAULT_AMOUNT, DEFAULT_AMOUNT_DEFAULT);
		}
		return defaultAmount;
	}
}
